package org.asl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * 
 */
public class BodyGestureActivity extends Activity {
    private static final int MENU_ASL_TO_SPEECH = 8;

    private static final int MENU_SPEECH_TO_ASL = 9;

    private static final int BODY_ZONE_HEAD = 0;
    private static final int BODY_ZONE_NECK = 1;
    private static final int BODY_ZONE_CHEST = 2;
    private static final int BODY_ZONE_RIGHTHAND = 3;
    private static final int BODY_ZONE_LEFTHAND = 4;
    private static final int BODY_ZONE_PALM = 5;
 
    private static int[][] bodyZoneColorMap = new int[6][3];

    private void initBodyZoneColorMap() {
    	//rgb color values 
    	bodyZoneColorMap[BODY_ZONE_HEAD] = new int[] {255, 247, 153};
    	bodyZoneColorMap[BODY_ZONE_NECK] = new int[] {253, 204, 235};
    	bodyZoneColorMap[BODY_ZONE_CHEST] = new int[] {148, 207, 143};
    	bodyZoneColorMap[BODY_ZONE_RIGHTHAND] = new int[] {255, 255, 255};
    	bodyZoneColorMap[BODY_ZONE_LEFTHAND] = new int[] {177, 254, 254};
    	bodyZoneColorMap[BODY_ZONE_PALM] = new int[] {177, 254, 254};
    }

    /**
     * Invoked during init to give the Activity a chance to set up its Menu.
     * 
     * @param menu the Menu to which entries may be added
     * @return true
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_ASL_TO_SPEECH, 0, R.string.asl_to_speech);
        menu.add(0, MENU_SPEECH_TO_ASL, 0, R.string.speech_to_asl);

        return true;
    }

    /**
     * Invoked when the user selects an item from the Menu.
     * 
     * @param item the Menu entry which was selected
     * @return true if the Menu item was legit (and we consumed it), false
     *         otherwise
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ASL_TO_SPEECH:
                return true;
            case MENU_SPEECH_TO_ASL:
                return true;
        }
        return false;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        initBodyZoneColorMap();
        //finger mapping: thumb -A, index -B, middle - C, ring - D, pinky - E
        Thread.setDefaultUncaughtExceptionHandler( new DefaultExceptionHandler());
        RelativeLayout linLayout = new RelativeLayout(this);
      
        Log.i("BodyGestureActivity", "Creating BodyGestureActivity...");
        int basePosLeft = 60;        
        int basePosTop = 60;
        //ImageView baseImageView = getImageView( R.drawable.bodyzones, 250, 350, basePosLeft, basePosTop, 0 );
        ImageView baseImageView = getImageView( R.drawable.bodyzones, 411, 581, basePosLeft, basePosTop, 0 );
        linLayout.addView(baseImageView);

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }

    public ImageView getImageView( int image_int, int sizeX, int sizeY, int posLeft, int posTop, int rotateAngle) {
        ImageView imageView = new ImageView(this);
        RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(sizeX, sizeY);
        layout.setMargins( posLeft, posTop, 0, 0);
        imageView.setLayoutParams(layout);
        
        if( rotateAngle == 0 ) {
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            Matrix matrix = new Matrix();
            //matrix.postScale(scaleWidth, scaleHeight);
            matrix.postRotate( rotateAngle);
            imageView.setScaleType(ImageView.ScaleType.MATRIX);
            imageView.setImageMatrix( matrix);
        }

        //imageView.setPadding(0, 0, 0, 0);
        imageView.setImageResource(image_int);
        
        imageView.setOnTouchListener(new View.OnTouchListener() {
        	
            @Override
            public boolean onTouch(View v, MotionEvent event) {
              Log.d("onTouch","position ["+v+"]");
              ImageView iv = (ImageView)v;
              int positionX = (int)event.getX();
              int positionY = (int)event.getY();              
              Log.d("onTouch","position= ["+ event.getX() +", "+ event.getY() + "]");

              /*
              Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bodyzones);
              int color1 = bitmap.getPixel(50, 50);
              Log.d("onTouch","color1 =  ["+color1+"]" );
              printRGB( color1);

              int color2 = bitmap.getPixel(100, 100);
              Log.d("onTouch","color2 =  ["+color2+"]");
              printRGB( color2);
			  */
              BitmapDrawable bd = (BitmapDrawable)iv.getDrawable(); 
              Log.d("onTouch","BitmapDrawable =  ["+bd+"]");

              Bitmap bitmap = bd.getBitmap(); 
              Log.d("onTouch","bitmap =  ["+bitmap+"]");
              //int color = bitmap.getPixel(positionX, positionY); 

              int color_selected = bitmap.getPixel(positionX, positionY);
              Log.d("onTouch","color_selected =  ["+color_selected+"]");
              int zoomed_resource = getBodyZoneSelected( color_selected);
              if( zoomed_resource != 0 ) {
                  iv.setImageResource( zoomed_resource);
              }
              
              return true;
            }
          });

        return imageView;
    }

    private int getBodyZoneSelected( int color) {
    	int red = Color.red(color);
    	int green = Color.green(color);
    	int blue =  Color.blue(color);
        Log.d("RGB","RGB of color["+ color + "] = {"+ Color.red(color) + ", "+ Color.green(color) + ", "+ Color.blue(color) + "}");
        
        int selected_body_zone = -1;
        for (int i=0; i<bodyZoneColorMap.length; i++) {
        	int[] rgb = bodyZoneColorMap[i];
        	if( red == rgb[0] && green == rgb[1] && blue == rgb[2] ) {
        		selected_body_zone = i;
        		break;
        	}
        }
        
        String selectedZone = "";
        int zoomed_resource = 0;
        if( selected_body_zone != -1 ) {
        	switch( selected_body_zone) {
            case BODY_ZONE_HEAD:  selectedZone = "HEAD"; zoomed_resource = R.drawable.headtopzones; break;
            case BODY_ZONE_NECK:  selectedZone = "NECK"; zoomed_resource = R.drawable.headbottomzones; break;
            case BODY_ZONE_CHEST:  selectedZone = "CHEST"; zoomed_resource = R.drawable.chestzones; break;
            case BODY_ZONE_RIGHTHAND:  selectedZone = "RIGHT HAND"; zoomed_resource = R.drawable.armzones; break;
            case BODY_ZONE_LEFTHAND:  selectedZone = "LEFT HAND"; zoomed_resource = R.drawable.armzones; break;
            case BODY_ZONE_PALM: selectedZone = "PALM"; zoomed_resource = R.drawable.armzones; break;
            default: selectedZone = "None"; break;
        	}
        }
        Log.d("BodyZone","Body Zone selected = ["+ selectedZone + "]");

        return zoomed_resource;
    }
    
    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     * 
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
    }
}

